package com.mingdinggroup.app.util;

import android.databinding.BindingAdapter;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.mingdinggroup.app.R;
import com.mingdinggroup.app.util.glide.GlideCircleTransform;
import com.mingdinggroup.app.util.glide.GlideRoundTransform;

import de.hdodenhof.circleimageview.CircleImageView;

public class DrawableUtils {


    @BindingAdapter({"imageUrl"})
    public static void loadImage(@Nullable ImageView imageView, String url) {
        if (TextUtils.isEmpty(url))
            return;
        Glide.with(imageView.getContext()).load(url).placeholder(R.drawable.ic_placeholder)
                .dontAnimate().centerCrop().into(imageView);
    }

    @BindingAdapter({"im_imageUrl"})
    public static void IMloadImage(@Nullable ImageView imageView, String url) {
        if (TextUtils.isEmpty(url))
            return;
        Glide.with(imageView.getContext()).load(url).dontAnimate().into(imageView);
    }

    @BindingAdapter({"imageUrlCircle"})
    public static void loadImageCircle(@Nullable ImageView imageView, String url) {
        if (TextUtils.isEmpty(url))
            return;
        Glide.with(imageView.getContext()).load(url).dontAnimate()
                .placeholder(R.drawable.ic_placeholder)
                .transform(new GlideCircleTransform(imageView.getContext())).into(imageView);
    }

    @BindingAdapter({"imageUrlCircleNewPreferences"})
    public static void loadImageCircleNewPreferences(@Nullable CircleImageView imageView,
                                                     String url) {
        /*if (TextUtils.isEmpty(url))
			return;
		Glide.with(imageView.getContext()).load(url).dontAnimate()

			.transform(new GlideCircleTransform(imageView.getContext()))
			.into(imageView);*/

        if (TextUtils.isEmpty(url))
            return;
        Glide.with(imageView.getContext()).load(url).placeholder(R.drawable.ic_placeholder)
                .into(new SimpleTarget<GlideDrawable>() {
                    @Override
                    public void onResourceReady(GlideDrawable resource,
                                                GlideAnimation<? super GlideDrawable> glideAnimation) {
                        imageView.setImageDrawable(resource);
                    }
                });


    }

    @BindingAdapter({"imageUrlRound"})
    public static void loadImageRound(@Nullable ImageView imageView, String url) {
        if (TextUtils.isEmpty(url))
            return;
        Glide.with(imageView.getContext()).load(url).dontAnimate()
                .transform(new GlideRoundTransform(imageView.getContext())).into(imageView);
    }


    @BindingAdapter({"imageUrl", "error", "placeholder"})
    public static void loadImage(ImageView imgView, String url, Drawable error,
                                 Drawable placeholder) {
        Glide.with(imgView.getContext()).load(url).error(error).placeholder(placeholder)
                .into(imgView);
    }


}
